<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="./src/vue.js"></script>
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <parent_com></parent_com>
    </div>
    <template id="parent">
      <div>
        <h3>父组件</h3>
        <p>p_name: {{p_name}}</p>
        <button @click="getSonInstance">获取子组件实例</button>
        <hr />
        <son_com></son_com>
      </div>
    </template>
    <template id="son">
      <div>
        <h3>子组件</h3>
        <p>s_name: {{s_name}}</p>
        <button @click="getParentInstance">获取父组件实例</button>
      </div>
    </template>
    <script>
      const son_com = {
        template: "#son",
        data() {
          return {
            s_name: "小小王",
          };
        },
        methods: {
          sSetName() {
            this.s_name = "小小明";
          },
          getParentInstance() {
            console.log(this.$parent);
            this.$parent.pSetName();
          },
        },
      };
      const parent_com = {
        template: "#parent",
        data() {
          return {
            p_name: "小王",
          };
        },
        methods: {
          pSetName() {
            this.p_name = "小明";
          },
          getSonInstance() {
            console.log(this.$children);
            this.$children[0].s_name = "笑笑笑王";
          },
        },
        components: {
          son_com,
        },
      };

      const vm = new Vue({
        el: "#app",
        data: {},
        components: {
          parent_com,
        },
      });
    </script>
  </body>
</html>
